package com.laoma.stockcenter.handler;

import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.laoma.ordercenter.common.CommonResponse;
import com.laoma.stockcenter.common.CommonException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author 老马
 * @apiNote
 * @date 2022-01-25 08:59
 **/
@SuppressWarnings("all")
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    //    //处理自定义的异常
    @ExceptionHandler(CommonException.class)
    public CommonResponse commonExceptionHandler(CommonException e) {
        log.error("全局异常捕获CommonException：{}", e.getMessage());
        return CommonResponse.buildFailCommonResponse(e.getMessage());
    }

    //处理流控异常
    @ExceptionHandler(BlockException.class)
    public CommonResponse blockExceptionHandler(BlockException e) {
        log.error("全局异常捕获BlockException：{}",e.getClass());
        return CommonResponse.buildFailCommonResponse("服务器繁忙，请稍后重试。");
    }

    //处理系统异常
    @ExceptionHandler(Exception.class)
    public CommonResponse exceptionHandler(Exception e) {
        log.error("全局异常捕获Exception：{}",e.getMessage());
        return CommonResponse.buildFailCommonResponse("系统异常");
    }
}
